<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4 0004
 * Time: 14:50
 */

namespace app\admin\controller;


use think\Db;

class Admin extends Base
{
    const TABLE_ADMIN = 'admin';
    public $table = 'admin';
    /**
     * 管理员列表
     */
    public function index()
    {
        $data = Db::name(self::TABLE_ADMIN)
            ->alias('a')
            ->join('rbac_role b','a.role_id=b.id','left')
            ->join('area c','a.province_id=c.id','left')
            ->join('area d','a.city_id=d.id','left')
            ->join('area e','a.county_id=e.id','left')
            ->join('area f','a.town_id=f.id','left')
            ->join('area g','a.village_id=g.id','left')
            ->field('a.*,b.name as role_name,c.name as name1,d.name as name2,e.name as name3,f.name as name4,g.name as name5')
            ->order('a.id desc')
            ->paginate();
        $page = $data->render();

        $this->assign('data',$data);
        $this->assign('page',$page);
        return $this->fetch();
    }


    /**
     * @return mixed
     * 添加管理员
     */
    public function add()
    {

        if(request()->isPost()){
            return $this->save();
        }
        $this->getRole();
        $this->getAdmin();
        $this->assign('data',1);
        $this->assign('title','新增管理员');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function save()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'role_id'=>input('role_id/d'),
            'province_id'=>input('province_id/d'),
            'city_id'=>input('city_id/d'),
            'county_id'=>input('county_id/d'),
            'town_id'=>input('town_id/d'),
            'village_id'=>input('village_id/d'),
            'remark'=>input('remark'),
            'modtime'=>time(),
        ];
        if(!$this->checkDataNode())
            return dataReturn(0,'数据权限错误');
        if($id){
            //编辑
            if($param['password'] != ''){
                $data['password'] = md5($param['password']);
            }
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            //新增
            $data['username'] = input('username');
            if($data['username'] == '')
                return dataReturn(0,'用户名不能为空');
            if(unique_add_value($data['username'],self::TABLE_ADMIN,'username')){
                $this->error('该用户名已存在');
            }
            $data['password'] = md5($param['password']);
            $data['addtime'] = time();
            $data['is_super'] = 0;
            $data['log_time'] = 0;
            $data['log_ip'] = '';
            $data['create_id'] = $this->admin['id'];
            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }


    /**
     * 修改管理员
     */
    public function update()
    {
        $id = input('id');
        $data = Db::name(self::TABLE_ADMIN)->where('id',$id)->find();
        if(!$data){
            $this->error('该用户不存在');
        }

        if(request()->isPost()){
            return $this->save();
        }
        $this->getRole();
        $this->getAdmin();
        $data['password'] = '';
        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    /**
     * ajax修改管理员状态
     */
    public function ajax_update()
    {
        $field = input('field');
        $value = input('value');
        $id = input('id');
        $data[$field] = $value;
        $data['modtime'] = time();
        $a = Db::name(self::TABLE_ADMIN)->where('id',$id)->update($data);
        if($a){
            echo api_return(1,'修改成功');
        }else{
            echo api_return(0,'修改失败');
        }

    }

    /**
     * 删除管理员
     */
    public function ajax_del()
    {
        $id = input('id');
        $a = Db::name(self::TABLE_ADMIN)->where('id',$id)->delete();
        if($a){
            echo api_return(1,'删除成功');
        }else{
            echo api_return(0,'删除失败');
        }
    }

    /**
     * 新添是否用户名唯一
     */
    public function ajax_unique_username()
    {
        $username = input('username');
        if(unique_add_value($username,self::TABLE_ADMIN,'username')){
            echo api_return(1,'已存在');
        }else{
            echo api_return(0,'不存在');
        };
    }



    public function getRole()
    {
        $role = Db::name('rbac_role')->order('id desc')->select();
        $this->assign('role',json_encode($role));
        return $role;

    }

}